//制定客户开发计划页面跳转
function Enact(chcId) {
    location.href = "/plan/enact/" + chcId;
}

//执行客户开发计划页面跳转
function Execute(chcId) {
    location.href = "/plan/execute/" + chcId;
}

//修改客户开发计划的计划项
function EditPlaTodo(obj, chcId) {
    var plaTodo = $(obj).parents("td").prev().children();

    alert(plaTodo.val());

    $.ajax({
        type : "get",
        url : "/plan/edit/plaTodo/" + chcId + "/" + plaTodo.val(),
        dataType : "json",
        success : function (data) {
            if (data.editResult == "true") {
                alert("保存成功!");
            } else if (data.editResult == "false") {
                alert("保存失败!");
            }
        }
    })
}

//添加一条客户开发计划信息
function Add(obj) {
    var plaChcId = $(obj).parents("table").prev().val();
    var plaTodo = $(obj).parents("td").prev().children().val();
    var plaDate = $(obj).parents("td").prev().prev().prev().text();

    var atr = $(obj).parents("form").prev().children().next();

    if (plaTodo == null || plaTodo == "") {
        alert("计划项不能为空!");
        return;
    }

    $.ajax({
        type : "get",
        url : "/plan/add",
        data : {'plaChcId' : plaChcId, 'plaTodo' : plaTodo},
        dataType : "json",
        success : function (data) {
            if (data.addResult == "true") {
                var plaId = data.addPlaId;

                atr.append("<tr role=\"row\" class=\"odd\">\n" +
                    "                                <td>" + plaDate + "</td>\n" +
                    "                                <td>\n" +
                    "                                    <input type=\"text\" name=\"plaTodo\" value=" + plaTodo + ">\n" +
                    "                                </td>\n" +
                    "                                <td>\n" +
                    "                                    <div class=\"btn-group\">\n" +
                    "                                        <a class=\"delPlan\" style=\"cursor: pointer\"\n" +
                    "                                           data-toggle=\"tooltip\" onclick=\"Del(this, " + plaId + ")\" \n" +
                    "                                           data-placement=\"top\" title=\"\"\n" +
                    "                                           data-original-title=\"删除\">删除</a>\n" +
                    "                                    </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
                    "                                    <div class=\"btn-group\">\n" +
                    "                                        <a class=\"editPlan\" style=\"cursor: pointer\"\n" +
                    "                                           data-toggle=\"tooltip\" onclick=\"EditPlaTodo(this, " + plaId + ")\"\n" +
                    "                                           data-placement=\"top\" title=\"\"\n" +
                    "                                           data-original-title=\"保存\">保存</a>\n" +
                    "                                    </div>\n" +
                    "                                </td>\n" +
                    "                            </tr>");
            } else if (data.addResult == "false") {
                alert("添加失败!");
            }
        }
    })
}

//删除一条客户开发计划信息
function Del(obj, plaId) {
    if (confirm("你确定要删除该客户开发计划信息？")) {
        $.ajax({
            type : "get",
            url : "/plan/del/" + plaId,
            dataType : "json",
            success : function (data) {
                if (data.delResult == "true") {
                    alert("删除成功!");
                    $(obj).parents("tr").remove();
                }
            },
            error : function (data) {
                alert("删除失败!");
            }
        })
    }
}

//修改客户开发计划的执行效果
function EditPlaResult(obj, plaId) {
    var plaResult = $(obj).parents("td").prev().children();

    if (plaResult.val() == null || plaResult.val() == "") {
        alert("执行效果不能为空!");
        return;
    }

    $.ajax({
        type : "get",
        url : "/plan/edit/plaResult/" + plaId + "/" + plaResult.val(),
        dataType : "json",
        success : function (data) {
            if (data.editResult == "true") {
                alert("保存成功!");
                plaResult.attr("disabled", "disabled");
            } else if (data.editResult == "false") {
                alert("保存失败!");
            }
        }
    })
}

//开发成功按钮
function Win(chcId) {
    var chcStatus = "3";

    $.ajax({
        type : "get",
        url : "/chance/edit/chcStatus",
        data : {"chcId" : chcId, "chcStatus" : chcStatus},
        dataType : "json",
        success : function (data) {
            if (data == -1) {
                alert("该销售机会的开发计划还未完成，不能归档");
            } else if (data > 0) {
                alert("开发成功，已归档");
                location.href = "/plan/list";
            }
        }
    })
}

//开发失败按钮
function Lose(chcId) {
    var chcStatus = "4";

    $.ajax({
        type : "get",
        url : "/chance/edit/chcStatus",
        data : {"chcId" : chcId, "chcStatus" : chcStatus},
        dataType : "json",
        success : function (data) {
            if (data == -1) {
                alert("该销售机会的开发计划还未完成，不能归档");
            } else if (data > 0) {
                alert("开发失败，已归档");
                location.href = "/plan/list";
            }
        }
    })
}